#include "tcpsev.h"
#include <QString>
#include <QTime>

TCPsev::TCPsev(QWidget *parent) : QWidget(parent)
{
    //mymenu = new Menu;
    mysever = new QTcpServer(this);
    mysocket = new QTcpSocket(this);
    if(!mysever->listen(QHostAddress::Any,8888))
    {
        qDebug() << "lisen error";
        assert(false);
    }


    connect(mysever,&QTcpServer::newConnection,this,[=]()
    {
        mysocket = mysever->nextPendingConnection();

        qDebug() << "ancji";
        connect(mysocket,&QTcpSocket::readyRead,this,[=]()
        {
            QByteArray arr = mysocket->readAll();
            //qDebug() << arr;
            int personDes = 1;
            QMessageBox::StandardButton mesPserson = QMessageBox::information(NULL,"Error","Have you made the correct imagine?",QMessageBox::Yes|QMessageBox::No);
            switch (mesPserson)
            {
            case QMessageBox::Yes:
                personDes = 1;
                break;
            case QMessageBox::No:
                personDes = 0;
                break;
            default:
                break;
            }
            qDebug() << arr.toUInt() << endl;


            if((arr.toUInt() == 1))
            {
                emit changeRoad(1,-1 , razon);

                //此处给定新提示
                int tmp_tip = generateTip()+1;

                if(tmp_tip == 1)
                {
                    if(  (map.data[(map.start.x()+1)][(map.start.y()-1)] == roadBlock) || (map.start.y()-1) >16 || (map.start.y()-1) <0)
                    {
                        tmp_tip = 2;
                    }
                }
                else if(tmp_tip == 2)
                {
                    if(  (map.data[(map.start.x()+1)][(map.start.y()+1)] == roadBlock) || (map.start.y()+1) >16 || (map.start.y()+1) <0)
                    {
                        tmp_tip = 1;
                    }
                }

                emit setTips(tmp_tip);


                emit noticSound();



                // s 表示发送给matlab的消息，应该包括两位数据，被试的决策(1,0)和label(1,2)

                QString s = QString("%1,%2").arg(personDes).arg(tmp_tip);

                qDebug() << QTime::currentTime();



                //qDebug() << s;

                mysocket->write(s.toUtf8());
            }
            else
            {

                emit changeRoad(1,1 , razon);

                int tmp_tip = generateTip()+1;

                if(tmp_tip == 1)
                {
                    if(  (map.data[(map.start.x()+1)][(map.start.y()-1)] == roadBlock) || (map.start.y()-1) >16 || (map.start.y()-1) <0)
                    {
                        tmp_tip = 2;
                    }
                }
                else if(tmp_tip == 2)
                {
                    if(  (map.data[(map.start.x()+1)][(map.start.y()+1)] == roadBlock) || (map.start.y()+1) >16 || (map.start.y()+1) <0)
                    {
                        tmp_tip = 1;
                    }
                }

                emit setTips(tmp_tip);

                emit noticSound();



                QString s = QString("%1,%2").arg(personDes).arg(tmp_tip);

                qDebug() << QTime::currentTime();



                //qDebug() << s;

                mysocket->write(s.toUtf8());
            }



        });

    });

    connect(mysever,&QTcpServer::acceptError,this,&TCPsev::dispErr);
}


void TCPsev::dispErr(QAbstractSocket::SocketError tErr)
{
    qDebug() << "one error has occured:" << tErr;
}

void TCPsev::send_start()
{
    mysocket->write("1,1");
}
void TCPsev::disconnectClient()
{
    mysocket->disconnectFromHost();
    mysocket->close();
}
